package com.gimi.cloud.bbp.client.service.userInfo.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gimi.cloud.bbp.client.service.userInfo.RdExpressInfoService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.dao.dao.User.RdExpressInfoMapper;
import com.gimi.cloud.bbp.dto.userInfo.RdExpressInfoDTO;
import com.gimi.cloud.bbp.entity.userInfo.RdExpressInfoEntity;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;

@Service
public class RdExpressInfoServiceImpl implements RdExpressInfoService {

	@Autowired
	private RdExpressInfoMapper userExpressDao;

	@Override
	public void addExpressInfo(RdExpressInfoDTO rdExpressInfoDTO) {
		RdExpressInfoEntity record = new RdExpressInfoEntity();
		BeanUtils.copyProperties(rdExpressInfoDTO, record);
		int i = userExpressDao.baseInsert(record);
		if (record.getIsDefault()==1) {
			modifyTheDefaultAddress(record);
		}
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID);
		}
	}

	@Override
	public RdExpressInfoEntity getExpressInfoSingle(RdExpressInfoDTO rdExpressInfoDTO) {
		if (StringUtil.isBlank(rdExpressInfoDTO.geteId()) || "".equals(rdExpressInfoDTO.geteId())) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		RdExpressInfoEntity selectByPrimaryKey = userExpressDao.selectByPrimaryKey(rdExpressInfoDTO.geteId());
		if (selectByPrimaryKey.getIsDelete()==1) {
			return null;
		}
		return userExpressDao.selectByPrimaryKey(rdExpressInfoDTO.geteId());
	}

	@Override
	public void updateExpressInfo(RdExpressInfoDTO rdExpressInfoDTO) {
		RdExpressInfoEntity record = new RdExpressInfoEntity();
		BeanUtils.copyProperties(rdExpressInfoDTO, record);
		int i = userExpressDao.updateByPrimaryKeySelective(record);
		if (record.getIsDefault()==1) {
			modifyTheDefaultAddress(record);
		}
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID);
		}
	}
	
	//根据user_id查询配送信息列表
	@Override
	public List<RdExpressInfoEntity> expressListByUser(RdExpressInfoDTO rdExpressInfoDTO) {
		if (rdExpressInfoDTO.getUserId()==null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return userExpressDao.expressListByUser(rdExpressInfoDTO.getUserId());
	}

	@Override
	public Integer expressDelete(RdExpressInfoDTO rdExpressInfoDTO,Boolean realdelete) {
		String eid = rdExpressInfoDTO.geteId();
		
		//如果被删除的地址是默认地址，设置用户最后添加的地址为默认地址
		RdExpressInfoEntity expressInfo = userExpressDao.selectByPrimaryKey(eid);
		if (expressInfo.getIsDefault()==1) {
			PageHelper.orderBy("add_time DESC");
			Example example = new Example(RdExpressInfoEntity.class);
			example.createCriteria().andEqualTo("userId", rdExpressInfoDTO.getUserId())
				.andEqualTo("isDelete",0);
			List<RdExpressInfoEntity> selectByExample = userExpressDao.selectByExample(example);
			if (selectByExample.size()>0) {
				RdExpressInfoEntity rdExpressInfoEntity = selectByExample.get(0);
				rdExpressInfoEntity.setIsDefault(1);
				userExpressDao.updateByPrimaryKeySelective(rdExpressInfoEntity);
			}
		}
		if (realdelete) {
			return userExpressDao.deleteByPrimaryKey(eid);
		}
		RdExpressInfoEntity record = new RdExpressInfoEntity();
		record.seteId(rdExpressInfoDTO.geteId());
		record.setIsDelete(1);
		record.setIsDefault(0);
		return userExpressDao.updateByPrimaryKeySelective(record);
	}
	
	private void modifyTheDefaultAddress(RdExpressInfoEntity rdExpressInfo) {
		RdExpressInfoEntity record = new RdExpressInfoEntity();
		record.setIsDefault(0);
		
		Example example = new Example(RdExpressInfoEntity.class);
		example.createCriteria().andEqualTo("userId", rdExpressInfo.getUserId())
			.andNotEqualTo("eId", rdExpressInfo.geteId());
		
		userExpressDao.updateByExampleSelective(record , example);
	}

}
